package com.marvels.approve.mqueues.controller;

import com.fintech.common.util.JsonUtil;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.manager.dto.ActivitiNodeDTO;
import com.marvels.approve.mqueues.service.ApproveILoanApplyDataSynchronizeService;
import com.marvels.approve.mqueues.service.impl.ApproveLoanApplyMqReciever;
import com.marvels.approve.process.dto.CompleteProcessDTO;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.ProcessApproveDTO;
import com.marvels.approve.process.service.ILoanApplyCaseService;
import com.marvels.approve.process.service.IProcessService;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.utils.MarvelsLogUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("approve/test_loan/service")
public class ApproveTestController {
	@Autowired
	private ApproveILoanApplyDataSynchronizeService loanApplyDataSynchronizeService;
	@Autowired
	private IProcessService processService;
	@Autowired
	private ILoanApplyCaseService loanApplyCaseService ;
	@Autowired
	 private com.marvels.approve.process.service.NodeFlowService nodeFlowService;
	@Autowired
	private ApproveLoanApplyMqReciever loanApplyMqReciever;
	
	@RequestMapping("testNodeFlowService")
	public void NodeFlowService(@RequestBody String loanId){
		nodeFlowService.selectViolateCode(loanId);
	}
//	
//	private com.dragon.loan.service.loanInfoIDragonService loanInfoIDragonService;
	@RequestMapping("testloan")
	public String test(@RequestBody  String loanId){
		try {
//			loanApplyDataSynchronizeService.synchronizeLoanApplyInfo("");
//			AcLoanApplyInfo ac = loanInfoIDragonService.queryLoanBasicInfo("17050617222455105");
			System.out.println(loanId +">>>>>>>>>>>>=====获取ac表中数据");
			JSONObject loanid = JsonUtil.strConvertJsonObj(loanId);
			String id = (String) loanid.get("loanId");
			McamLoanApplyInfoDTO ac = loanApplyDataSynchronizeService.synchronizeLoanApplyInfo(id);
			MarvelsLogUtil.getInstance().info("@@@@@@@@@@@@@@@@loan:"+ac);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("",e);
		}
		return "test";
	}
	
	@RequestMapping("testProcess")
	public String testProcess(@RequestBody LoanApplyCaseDTO loanApplyCaseDTO){
		try {
			List<LoanApplyCaseDTO> cases = loanApplyCaseService.findLoanApplyCaseForTransfers(loanApplyCaseDTO);
			if(cases!=null && cases.size()>0) {
				LoanApplyCaseDTO oneCase = cases.get(0);
				CompleteProcessDTO cp = new CompleteProcessDTO();
				cp.setLoanId(oneCase.getLoanId());
				ActivitiNodeDTO dto = new ActivitiNodeDTO();
				dto.setNodeCode(oneCase.getCurNodeCode());
				dto.setNodeName(dto.getNodeName());
				cp.setCurNode(dto);
				ProcessApproveDTO variables = new ProcessApproveDTO();
				variables.setProcessResult(MarvelsConstant.PROCESS_RESULT_OK);
//				variables.put("processResult", MarvelsConstant.PROCESS_RESULT_OK);
//				cp.setVariables(variables);
				cp.setProcessApproveDto(variables);
				LoanApplyCaseDTO  caseDTO = processService.completeLoanApplyCase(cp);
				MarvelsLogUtil.getInstance().info("@@@@@@@@@@@@@@@@loan:"+caseDTO);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("",e);
		}
		return "testProcess";
	}
	
	@RequestMapping("process/{loanId}")
	public void process(@PathVariable("loanId") String loanId){
		try {
			loanApplyMqReciever.process(loanId);
			} catch(Exception e) {
			MarvelsLogUtil.getInstance().error("",e);
		}
	}
	
	
}
